package internationalenergytradingplatform.controller.BuyerController;

import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.BuyerAccountDao;
import internationalenergytradingplatform.pojo.BuyerAccount;
import internationalenergytradingplatform.pojo.BuyerBalance;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/buyer-banlance")

public class BuyerBanlanceController {

    @Autowired
    BuyerAccountDao buyerAccountDao;

    /**
     * 余额充值
     */
    @PutMapping
    public Result<Object> recharge(double rechargeMoney){
        String buyerId = BaseContext.getCurrentId();//获取手机号
        BuyerAccount buyerByTele = buyerAccountDao.getBuyerByTele(buyerId);
        buyerAccountDao.updateBalance(buyerByTele.getBalance() + rechargeMoney, buyerId);
        BuyerBalance buyerBalance = new BuyerBalance();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        buyerBalance.setTime(formattedDateTime);
        buyerBalance.setType(1);
        buyerBalance.setAccount(buyerId);
        buyerBalance.setBalance(rechargeMoney);
        buyerAccountDao.insertUpdateBalance(buyerBalance);
        return Result.success(buyerByTele.getBalance() + rechargeMoney);
    }
}
